<!DOCTYPE HTML>
<html>
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <meta name="keywords" content="关键词">
    <meta name="description" content="描述">
    <meta name="author" content="潭州教育-阿飞老师">
    <style>
        body{font-family: "Microsoft YaHei",serif;}
        body,dl,dd,p,h1,h2,h3,h4,h5,h6{margin:0;}
        ol,ul,li{margin:0;padding:0;list-style:none;}
        img{border:none;}

        body{
            user-select: none;
        }

        #wrap{
            overflow: hidden;
            position: relative;
            width: 488px;
            height: 600px;
            border: 1px solid pink;
            margin: 20px auto 0;
        }
        #wrap .scroll{
            position: absolute;
            top: 0;
            right: 0;
            width: 12px;
            height: 100%;
            background: #ddd;
        }
        #wrap .scroll .bar{
            position: absolute;
            top: 0;
            left: 0;
            width: 100%;
            height: 100px;
            background-color: #888;
        }
        #wrap .content{
            position: absolute;
            top: 0;
            left: 0;
        }
        #wrap .content li{
            background-color: #eee;
            line-height: 30px;
            margin-bottom: 1px;
            padding: 0 12px;
        }
        #wrap .content li span{
            margin-left: 20px;
        }
    </style>
</head>
<body>
    <div id="wrap">
        <!--内容-->
        <ul class="content">
            <li>
                <p>1、Life was like a box of chocolates, you never know what you're going to get.</p>
                <span>生命就像一盒巧克力，结果往往出人意料。</span>
            </li>
            <li>
                <p>2、Stupid is as stupid does.</p>
                <span>蠢人做蠢事，也可理解为傻人有傻福。</span>
            </li>
            <li>
                <p>3、Miracles happen every day.</p>
                <span>奇迹每天都在发生。</span>
            </li>
            <li>
                <p>4、it made me look like a duck in water.</p>
                <span>它让我如鱼得水。</span>
            </li>
            <li>
                <p>5、I don't know if we each have a destiny, or if we're all just floating around accidental—like on a breeze.</p>
                <span>我不懂我们是否有着各自的命运，还是只是到处随风飘荡。</span>
            </li>
            <li>
                <p>6、Death is just a part of life, something we're all destined to do.</p>
                <span>死亡是生命的一部分，是我们注定要做的一件事。</span>
            </li>
            <li>
                <p>7、You have got to put the past behind you before you can move on.</p>
                <span>放下包袱，继续前进。</span>
            </li>
            <li>
                <p>8、It was like just before the sun goes to bed down on the bayou. There was a million sparkles on the river.</p>
                <span>就像太阳在落山前映射在河口上，有无数的亮点在闪闪发光。</span>
            </li>
            <li>
                <p>9、If there is anything you need, I will not be far away.</p>
                <span>只要你需要，我就在这里。</span>
            </li>
            <li>
                <p>1、Life was like a box of chocolates, you never know what you're going to get.</p>
                <span>生命就像一盒巧克力，结果往往出人意料。</span>
            </li>
            <li>
                <p>2、Stupid is as stupid does.</p>
                <span>蠢人做蠢事，也可理解为傻人有傻福。</span>
            </li>
            <li>
                <p>3、Miracles happen every day.</p>
                <span>奇迹每天都在发生。</span>
            </li>
            <li>
                <p>4、it made me look like a duck in water.</p>
                <span>它让我如鱼得水。</span>
            </li>
            <li>
                <p>5、I don't know if we each have a destiny, or if we're all just floating around accidental—like on a breeze.</p>
                <span>我不懂我们是否有着各自的命运，还是只是到处随风飘荡。</span>
            </li>
            <li>
                <p>6、Death is just a part of life, something we're all destined to do.</p>
                <span>死亡是生命的一部分，是我们注定要做的一件事。</span>
            </li>
            <li>
                <p>7、You have got to put the past behind you before you can move on.</p>
                <span>放下包袱，继续前进。</span>
            </li>
            <li>
                <p>8、It was like just before the sun goes to bed down on the bayou. There was a million sparkles on the river.</p>
                <span>就像太阳在落山前映射在河口上，有无数的亮点在闪闪发光。</span>
            </li>
            <li>
                <p>9、If there is anything you need, I will not be far away.</p>
                <span>只要你需要，我就在这里。</span>
            </li>
            <li>
                <p>1、Life was like a box of chocolates, you never know what you're going to get.</p>
                <span>生命就像一盒巧克力，结果往往出人意料。</span>
            </li>
            <li>
                <p>2、Stupid is as stupid does.</p>
                <span>蠢人做蠢事，也可理解为傻人有傻福。</span>
            </li>
            <li>
                <p>3、Miracles happen every day.</p>
                <span>奇迹每天都在发生。</span>
            </li>
            <li>
                <p>4、it made me look like a duck in water.</p>
                <span>它让我如鱼得水。</span>
            </li>
            <li>
                <p>5、I don't know if we each have a destiny, or if we're all just floating around accidental—like on a breeze.</p>
                <span>我不懂我们是否有着各自的命运，还是只是到处随风飘荡。</span>
            </li>
            <li>
                <p>6、Death is just a part of life, something we're all destined to do.</p>
                <span>死亡是生命的一部分，是我们注定要做的一件事。</span>
            </li>
            <li>
                <p>7、You have got to put the past behind you before you can move on.</p>
                <span>放下包袱，继续前进。</span>
            </li>
            <li>
                <p>8、It was like just before the sun goes to bed down on the bayou. There was a million sparkles on the river.</p>
                <span>就像太阳在落山前映射在河口上，有无数的亮点在闪闪发光。</span>
            </li>
            <li>
                <p>9、If there is anything you need, I will not be far away.</p>
                <span>只要你需要，我就在这里。</span>
            </li>
            <li>
                <p>1、Life was like a box of chocolates, you never know what you're going to get.</p>
                <span>生命就像一盒巧克力，结果往往出人意料。</span>
            </li>
            <li>
                <p>2、Stupid is as stupid does.</p>
                <span>蠢人做蠢事，也可理解为傻人有傻福。</span>
            </li>
            <li>
                <p>3、Miracles happen every day.</p>
                <span>奇迹每天都在发生。</span>
            </li>
            <li>
                <p>4、it made me look like a duck in water.</p>
                <span>它让我如鱼得水。</span>
            </li>
            <li>
                <p>5、I don't know if we each have a destiny, or if we're all just floating around accidental—like on a breeze.</p>
                <span>我不懂我们是否有着各自的命运，还是只是到处随风飘荡。</span>
            </li>
            <li>
                <p>6、Death is just a part of life, something we're all destined to do.</p>
                <span>死亡是生命的一部分，是我们注定要做的一件事。</span>
            </li>
            <li>
                <p>7、You have got to put the past behind you before you can move on.</p>
                <span>放下包袱，继续前进。</span>
            </li>
            <li>
                <p>8、It was like just before the sun goes to bed down on the bayou. There was a million sparkles on the river.</p>
                <span>就像太阳在落山前映射在河口上，有无数的亮点在闪闪发光。</span>
            </li>
            <li>
                <p>9、If there is anything you need, I will not be far away.</p>
                <span>只要你需要，我就在这里。</span>
            </li>
            <li>
                <p>1、Life was like a box of chocolates, you never know what you're going to get.</p>
                <span>生命就像一盒巧克力，结果往往出人意料。</span>
            </li>
            <li>
                <p>2、Stupid is as stupid does.</p>
                <span>蠢人做蠢事，也可理解为傻人有傻福。</span>
            </li>
            <li>
                <p>3、Miracles happen every day.</p>
                <span>奇迹每天都在发生。</span>
            </li>
            <li>
                <p>4、it made me look like a duck in water.</p>
                <span>它让我如鱼得水。</span>
            </li>
            <li>
                <p>5、I don't know if we each have a destiny, or if we're all just floating around accidental—like on a breeze.</p>
                <span>我不懂我们是否有着各自的命运，还是只是到处随风飘荡。</span>
            </li>
            <li>
                <p>6、Death is just a part of life, something we're all destined to do.</p>
                <span>死亡是生命的一部分，是我们注定要做的一件事。</span>
            </li>
            <li>
                <p>7、You have got to put the past behind you before you can move on.</p>
                <span>放下包袱，继续前进。</span>
            </li>
            <li>
                <p>8、It was like just before the sun goes to bed down on the bayou. There was a million sparkles on the river.</p>
                <span>就像太阳在落山前映射在河口上，有无数的亮点在闪闪发光。</span>
            </li>
            <li>
                <p>9、If there is anything you need, I will not be far away.</p>
                <span>只要你需要，我就在这里。</span>
            </li>
        </ul>
        <!--滚动条-->
        <div class="scroll">
            <div class="bar"></div>
        </div>
    </div>

    <script>

        (function(){
            //重要变量
            let oScroll = document.querySelector("#wrap .scroll"),
                oWrap = document.getElementById("wrap"),
                oBar = document.querySelector("#wrap .scroll .bar"),
                oContent = document.querySelector("#wrap .content");
            let
                //wrap的高度 = scroll的高度
                wrapH = oScroll.offsetHeight,
                //wrapH / contentH
                prop = wrapH/oContent.offsetHeight
            ;

            //根据内容大小，求出滑块高度
            (function(){
                let height = wrapH*prop;
                oBar.style.height = height + 'px';
            })();

            //如果说 content比wrap小，就不需要滑块了
            if( prop >= 1 ){
                oScroll.style.display = "none";
                return ;
            }

            //各种事件的添加
            (function(){
                //公共变量
                let downY, //按下时的y坐标
                    downTop, //按下时的top值
                    barMax = wrapH - oBar.offsetHeight, //最大top
                    barMin = 0, //最小top
                    contentMax = oContent.offsetHeight - wrapH, //content最大top
                    scrollStep = 10*barMax/contentMax
                ;

                //按下事件
                oBar.addEventListener("mousedown" , barDown);
                //抬起事件
                document.addEventListener("mouseup" , docUp);
                //滚轮事件
                oWrap.addEventListener("mousewheel" , scrollFn);

                //bar的down事件
                function barDown(ev){
                    //按下时的Y坐标
                    downY = ev.clientY;
                    downTop = oBar.offsetTop;
                    document.addEventListener("mousemove" ,docMove);
                }
                //document的move事件
                function docMove(ev){
                    //鼠标位置的变化量
                    let y_ = ev.clientY - downY;
                    let bTop = downTop + y_;

                    //执行变化函数
                    changeTop(bTop);
                }
                //document的up事件
                function docUp(){
                    document.removeEventListener("mousemove" , docMove);
                }
                //滚动事件
                function scrollFn(ev) {
                    //方位修正，往上-  往下+
                    let d = ev.wheelDelta/120;
                    //执行变化
                    changeTop( oBar.offsetTop - d*scrollStep );

                    ev.preventDefault();
                }

                //改变函数
                function changeTop(bTop){
                    //极限值的判断
                    bTop = Math.min(bTop,barMax);
                    bTop = Math.max(bTop,barMin);

                    //求对应的content的位置
                    let cTop = bTop*contentMax/barMax;

                    //位置改变
                    oBar.style.top = bTop + 'px';
                    oContent.style.top = -cTop + 'px';
                }
            })();

        })();
    </script>
</body>
</html>














